programming4us
           
 
 
SQL Server

SQL server 2008 : Handling Errors (part 2)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/3/2010 9:07:53 AM
Example: Handling Errors with @@ERROR Function

In this example, we will develop a stored procedure that inserts a new credit card in the AdventureWorks2008 database and handle its output with an @@ERRORFigure 1 shows the structure of the CreditCard table. function and some user-defined error messages.

Figure 1. Structure of CreditCard Table


To standardize your database messages and make management efficient, you decide to create two user-defined messages: one for the successful insertion of a credit card and the other for an error that arises in case of failure. Figure 2 shows the syntax of these messages.

Figure 2. Creating User-Defined Error Messages Using sp_addmessage


You can see that the severity level of these messages follows the SQL Server standard for system messages, providing organization for them. After you add the messages, the next step is the creation of your new procedure. Based on the CreditCard table structure, you can create a procedure that receives four input parameters. Figure 3 shows the syntax of a new uspInsertValidatedCreditCard procedure.

Figure 3. Creating the uspInsertValidatedCreditCard Procedure


Let’s look at this code in detail. After you define the header part of the procedure, you start the routine of the procedure declaring a local variable called @Error. This local variable will store the @@ERROR value. Then, after the INSERT statement, you retrieve the @@ERROR value and set it as the @Error value. With the variable value defined, you can now analyze it using an IF statement. If the value of the variable is 0, then there is no error and the success message will be raised. Otherwise, an error will arise and your custom error message will be shown.

Figure 4 shows the uspInsertValidatedCreditCard procedure being called twice and the last three CreditCard table rows. The first call will return the successful message, while the second will raise an error caused by a duplicate key violation in the credit card number column.

Figure 4. Testing the uspInsertValidatedCreditCard Procedure


Other -----------------
- SQL Server 2008 : Indexing for Performance - Putting It All Together (part 5) - Filtered Indexes
- SQL Server 2008 : Indexing for Performance - Putting It All Together (part 4) - Indexing JOIN Criteria
- SQL Server 2008 : Indexing for Performance - Putting It All Together (part 3) - Covering Your Queries
- SQL Server 2008 : Indexing for Performance - Putting It All Together (part 2) - Clustered Index Seeks
- SQL Server 2008 : Indexing for Performance - Putting It All Together (part 1)
- SQL Server Integration Services : Logged and Nonlogged Operations
- SQL Server Integration Services : Using bcp (part 5)
- SQL Server Integration Services : Using bcp (part 4)
- SQL Server Integration Services : Using bcp (part 3)
- SQL Server Integration Services : Using bcp (part 2) - Fundamentals of Exporting and Importing Data
- SQL Server Integration Services : Using bcp (part 1)
- SQL Server Integration Services : Connection Projects in Visual Studio
- SQL Server Integration Services : The Package Execution Utility (part 3) - The dtutil Utility
- SQL Server Integration Services : The Package Execution Utility (part 2) - Running Packages
- SQL Server Integration Services : The Package Execution Utility (part 1)
- SQL Server Integration Services : The SSIS Designer
- SQL Server Integration Services : Running the SSIS Wizard
- SQL Server Integration Services : A Data Transformation Requirement
- SQL Server 2008 : SSIS Tools and Utilities
- SQL Server 2008 : SSIS Architecture and Concepts
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us